package icu.xblogs.report.controller;

import icu.xblogs.report.domain.Employee;
import icu.xblogs.report.service.AttendanceRecordService;
import icu.xblogs.report.service.EmployeeService;
import icu.xblogs.report.utils.CookieUtil;
import icu.xblogs.report.utils.FileConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Frank KONG
 * @version 1.0
 * @date 2020/11/17 10:02
 *
 * 该Controller下请求均需要登录后才可以处理
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private EmployeeService employeeService;

    private AttendanceRecordService attendanceRecordService;

    @Autowired
    @Qualifier("employeeService")
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @Autowired
    @Qualifier("attendanceRecordService")
    public void setAttendanceRecordService(AttendanceRecordService attendanceRecordService) {
        this.attendanceRecordService = attendanceRecordService;
    }

    @RequestMapping("/test")
    public void test(){
        System.out.println("employee test...");
    }

    @RequestMapping(value = "/getId", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getId(HttpServletRequest request){
        HashMap<String, String> hashMap = CookieUtil.getCookie(request, "id");
        return hashMap.get("id");
    }

    @RequestMapping(value = "/getAttendanceByDate", produces = "application/json")
    @ResponseBody
    public Map<String, String> getAttendanceByDate(HttpServletRequest request, String date){
        return attendanceRecordService.getOneDay(date, Integer.parseInt(CookieUtil.getCookie(request, "id").get("id")));
    }

    @RequestMapping(value = "/getAttendanceByMonth", produces = "application/json")
    @ResponseBody
    public Map<String, String> getAttendanceByMonth(HttpServletRequest request, String month){
        return attendanceRecordService.getOneMonth(month, Integer.parseInt(CookieUtil.getCookie(request, "id").get("id")));
    }

    @RequestMapping(value = "/modifyPassword", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String modifyPassword(HttpServletRequest request, String oldPassword, String newPassword){
        employeeService.updatePsw(Integer.parseInt(CookieUtil.getCookie(request, "id").get("id")), oldPassword, newPassword);
        return "success";
    }

    @RequestMapping(value = "/getImg", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getImg(HttpServletRequest request){
        return employeeService.getImg(Integer.parseInt(CookieUtil.getCookie(request, "id").get("id")));
    }

    @RequestMapping(value = "/updateImg", produces = "application/json")
    @ResponseBody
    public Map<String, String> updateImg(@RequestParam(value = "image-file", required = true) MultipartFile file, HttpServletRequest request){
        String id = CookieUtil.getCookie(request, "id").get("id");

        HashMap<String, String> hashMap = new HashMap<>();

        if(file.isEmpty()){
            hashMap.put("url", "");
            hashMap.put("success", "0");
            hashMap.put("message", "图片不能为空");

            return hashMap;
        }

        if(file.getSize() > FileConstants.MAX_IMG_SIZE){
            hashMap.put("url", "");
            hashMap.put("success", "0");
            hashMap.put("message", "图片过大！最大限制为3M.");

            return hashMap;
        }

        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if(!".jpg".equals(suffix)){
            hashMap.put("url", "");
            hashMap.put("success", "0");
            hashMap.put("message", "不支持的图片格式！请上传.jpg格式");

            return hashMap;
        }

        String fileName = id + suffix;
        File targetFile = new File(FileConstants.PIC_PATH, fileName);
        try {
            file.transferTo(targetFile);
            String urlPath = "https://xblogs.icu/file/employeeimg/" + fileName;
            employeeService.updateImg(urlPath, Integer.parseInt(id));
            hashMap.put("url", urlPath);
            hashMap.put("success", "1");
            hashMap.put("message", "upload success!");
            return hashMap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
}
